package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.core.entity.BaseDO;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IApplyToProgDepDAO;
import org.jsola.hr.dto.ApplyToProgDepAddDTO;
import org.jsola.hr.dto.ApplyToProgDepUpdateDTO;
import org.jsola.hr.entity.ApplyToProgDepDO;
import org.jsola.hr.query.ApplyToProgDepQuery;
import org.jsola.hr.service.IApplyToProgDepService;
import org.jsola.hr.vo.ApplyToProgDepListVO;
import org.jsola.hr.vo.ApplyToProgDepVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrApplyToProgDepServiceImpl")
public class ApplyToProgDepServiceImpl implements IApplyToProgDepService {

    @Autowired
    private IApplyToProgDepDAO applyToProgDepDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ApplyToProgDepVO save(ApplyToProgDepAddDTO applyToProgDepAddDTO, TokenUser tokenUser) {
        // 转DO
        ApplyToProgDepDO applyToProgDepDO = applyToProgDepAddDTO.to(ApplyToProgDepDO.class);
        // 保存
        applyToProgDepDO = save(applyToProgDepDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增方案适用设置部门", "saveApplyToProgDep", applyToProgDepDO.getId(), tokenUser);
        return applyToProgDepDO.to(ApplyToProgDepVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(ApplyToProgDepUpdateDTO applyToProgDepUpdateDTO, TokenUser tokenUser) {
        //转DO
        ApplyToProgDepDO applyToProgDepDO = applyToProgDepUpdateDTO.to(ApplyToProgDepDO.class);
        //根据主键更新，只更新非null值
        HrLogKit.logForUpdate("修改方案适用设置部门", "updateApplyToProgDep", applyToProgDepDO.getId(), tokenUser);
        return updateByIdSelective(applyToProgDepDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... applyToProgDepIds) {
        HrLogKit.logForDelete("批量删除方案适用设置部门", "deleteApplyToProgDepByIds"
                , Arrays.toString(applyToProgDepIds), tokenUser);
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) applyToProgDepIds);
    }


    @Override
    public ApplyToProgDepVO selectById(String applyToProgDepId, String siteId) {
        ApplyToProgDepDO applyToProgDepDO = selectDOById(applyToProgDepId, siteId);
        if (applyToProgDepDO == null) {
            return null;
        }
        return applyToProgDepDO.to(ApplyToProgDepVO.class);
    }

    @Override
    public List<ApplyToProgDepListVO> select(ApplyToProgDepQuery applyToProgDepQuery, String siteId) {
        List<ApplyToProgDepDO> applyToProgDepDOList = selectDO(applyToProgDepQuery, siteId);
        if (CollectionUtils.isEmpty(applyToProgDepDOList)) {
            return applyToProgDepDOList == null ? null : new ArrayList<>();
        }
        return applyToProgDepDOList.stream()
                .map(applyToProgDepDO -> applyToProgDepDO.to(ApplyToProgDepListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ApplyToProgDepQuery applyToProgDepQuery, String siteId) {
        Example example = buildExample(applyToProgDepQuery, siteId);
        return applyToProgDepDAO.selectCountByExample(example);
    }

    @Override
    public Page<ApplyToProgDepListVO> selectPage(ApplyToProgDepQuery applyToProgDepQuery, String siteId) {
        Example example = buildExample(applyToProgDepQuery, siteId);
        Page<ApplyToProgDepDO> page = applyToProgDepDAO.selectPageByExample(example,
                applyToProgDepQuery.getPageNo(),
                applyToProgDepQuery.getPageSize());

        return page.to(ApplyToProgDepListVO.class);
    }

    @Override
    public ApplyToProgDepDO selectDOById(String applyToProgDepId, String siteId) {
        return listById(applyToProgDepId, siteId);
    }

    @Override
    public List<ApplyToProgDepDO> selectDO(ApplyToProgDepQuery applyToProgDepQuery, String siteId) {
        Example example = buildExample(applyToProgDepQuery, siteId);
        return applyToProgDepDAO.selectByExample(example);
    }

    @Override
    public Integer insertList(List<ApplyToProgDepAddDTO> applyToProgDepAddDTOList, String progId, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(applyToProgDepAddDTOList)) {
            return 0;
        }
        List<ApplyToProgDepDO> applyToProgDepDOList = applyToProgDepAddDTOList.stream().map(applyToProgDepAddDTO -> {
            ApplyToProgDepDO applyToProgDepDO = applyToProgDepAddDTO.to(ApplyToProgDepDO.class);
            applyToProgDepDO.setSiteId(tokenUser.getSiteId());
            applyToProgDepDO.setCreateUserId(tokenUser.getUserId());
            applyToProgDepDO.setGmtCreate(new Date());
            applyToProgDepDO.setProgId(progId);
            return applyToProgDepDO;
        }).collect(Collectors.toList());
        HrLogKit.logForSave("批量新增方案适用设置部门", "insertListApplyToProgDep", applyToProgDepDOList
                .parallelStream().map(BaseDO::getId).collect(Collectors.joining(",", "[", "]")), tokenUser);
        return applyToProgDepDAO.insertListAndSetId(applyToProgDepDOList);
    }

    @Override
    public Integer deleteByProgId(String progId, TokenUser tokenUser) {
        ApplyToProgDepDO applyToProgDepDO = new ApplyToProgDepDO();
        applyToProgDepDO.setProgId(progId);
        applyToProgDepDO.setSiteId(tokenUser.getSiteId());
        HrLogKit.logForDelete("根据方案id删除方案适用设置部门（真实删除）", "deleteByProgId", progId, tokenUser);
        return applyToProgDepDAO.delete(applyToProgDepDO);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param applyToProgDepQuery 查询参数
     * @param siteId              所属站点id
     * @return example
     */
    private Example buildExample(ApplyToProgDepQuery applyToProgDepQuery, String siteId) {
        Example example = new Example(ApplyToProgDepDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (applyToProgDepQuery.getProgId() != null) {
            example.and().andLike("progId", "%" + applyToProgDepQuery.getProgId() + "%");
        }
        if (applyToProgDepQuery.getDepId() != null) {
            example.and().andLike("depId", "%" + applyToProgDepQuery.getDepId() + "%");
        }
        if (applyToProgDepQuery.getType() != null) {
            example.and().andEqualTo("type", applyToProgDepQuery.getType());
        }
        if (applyToProgDepQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", applyToProgDepQuery.getCompanyId());
        }
        if (applyToProgDepQuery.getGmtCreateStart() != null &&
                applyToProgDepQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", applyToProgDepQuery.getGmtCreateStart(),
                    applyToProgDepQuery.getGmtCreateEnd());
        }
        if (applyToProgDepQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + applyToProgDepQuery.getCreateUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example, applyToProgDepQuery.getOrders());
        return example;
    }
}




